<template>
  <div class="forgot-password-container">
    <!-- 背景层 -->
    <div class="background-layer"></div>

    <!-- 忘记密码表单卡片 -->
    <div class="forgot-password-card">
      <div class="logo-container">
        <img src="@/assets/logo.svg" alt="智源工坊" class="logo" />
        <h1 class="system-name">智源工坊</h1>
        <p class="page-title">找回密码</p>
      </div>

      <a-form
        :model="formState"
        name="forgotPassword"
        autocomplete="off"
        @finish="handleSubmit"
        class="forgot-password-form"
      >
        <!-- 账号输入 -->
        <a-form-item name="userAccount" :rules="[{ required: true, message: '请输入账号' }]">
          <a-input v-model:value="formState.userAccount" placeholder="请输入账号" size="large">
            <template #prefix>
              <UserOutlined style="color: rgba(0, 0, 0, 0.25)" />
            </template>
          </a-input>
        </a-form-item>

        <!-- 邮箱输入 -->
        <a-form-item
          name="email"
          :rules="[
            { required: true, message: '请输入邮箱' },
            { type: 'email', message: '请输入正确的邮箱格式' },
          ]"
        >
          <a-input v-model:value="formState.email" placeholder="请输入邮箱" size="large">
            <template #prefix>
              <MailOutlined style="color: rgba(0, 0, 0, 0.25)" />
            </template>
          </a-input>
        </a-form-item>

        <!-- 验证码输入 -->
        <a-form-item name="captcha" :rules="[{ required: true, message: '请输入验证码' }]">
          <a-row :gutter="8">
            <a-col :span="14">
              <a-input v-model:value="formState.captcha" placeholder="请输入验证码" size="large">
                <template #prefix>
                  <SafetyOutlined style="color: rgba(0, 0, 0, 0.25)" />
                </template>
              </a-input>
            </a-col>
            <a-col :span="10">
              <div class="captcha-image" @click="refreshCaptcha" >
                <img :src="captchaSrc" alt="验证码" title="点击刷新验证码" width="130px" height="40px" style="margin-top: 5px"/>
              </div>
            </a-col>
          </a-row>
        </a-form-item>

        <!-- 提交按钮 -->
        <a-form-item>
          <a-button
            type="primary"
            html-type="submit"
            size="large"
            :loading="loading"
            class="submit-button"
          >
            发送重置邮件
          </a-button>
        </a-form-item>

        <!-- 返回登录链接 -->
        <div class="back-to-login">
          想起密码了? <RouterLink to="/user/login" class="login-link">返回登录</RouterLink>
        </div>
      </a-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue'
import { UserOutlined, MailOutlined, SafetyOutlined } from '@ant-design/icons-vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { forgetPwd } from '@/api/userController'
import { API_BASE_URL } from '@/config/env'

const router = useRouter()
const loading = ref(false)
/**
 * 验证码相关
 */
const fetchCaptcha = API_BASE_URL + '/captcha/image'
const captchaSrc = ref<string>(fetchCaptcha)
//刷新验证码
const refreshCaptcha = ()=>{
  captchaSrc.value = fetchCaptcha + `?${new Date().getTime()}`;
}
const formState = reactive<API.EditForForgetPwdRequest>({
  userAccount: '',
  email: '',
  captcha: '',
})


// 处理表单提交
const handleSubmit = async (values: API.EditForForgetPwdRequest) => {
  // 验证验证码
  if (values.captcha?.toLowerCase() !== '') {
    message.error('请输入验证码')
    refreshCaptcha()
    return
  }

  loading.value = true
  try {
    const res = await forgetPwd(values)

    if (res.data.code === 0) {
      message.success('重置密码邮件已发送到您的邮箱，请查收')

      // 跳转到登录页
      setTimeout(() => {
        router.push('/user/login')
      }, 1000)
    } else {
      message.error('发送失败: ' + res.data.msg)
      refreshCaptcha()
    }
  } catch (error) {
    message.error('发送失败，请稍后重试')
    refreshCaptcha()
  } finally {
    loading.value = false
  }
}


</script>

<style scoped>
.forgot-password-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 90vh;
  background: url('@/assets/login2.png') no-repeat center center fixed;
  background-size: cover;
  position: relative;
  overflow: hidden;
}

.background-layer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  filter: blur(8px);
  z-index: 0;
}

.forgot-password-card {
  width: 420px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
  z-index: 1;
  position: relative;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.forgot-password-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 8px;
  background: linear-gradient(90deg, #ff7875 0%, #ffa940 100%);
}

.logo-container {
  text-align: center;
  margin-bottom: 30px;
}

.logo {
  width: 80px;
  height: 80px;
  margin-bottom: 15px;
}

.system-name {
  font-size: 22px;
  color: #1890ff;
  margin-bottom: 8px;
  font-weight: 600;
}

.page-title {
  font-size: 16px;
  color: #666;
  margin-bottom: 0;
  font-weight: 400;
}

.forgot-password-form {
  margin-top: 20px;
}

.forgot-password-form :deep(.ant-input-affix-wrapper) {
  padding: 10px 15px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.7);
}

.forgot-password-form :deep(.ant-input) {
  font-size: 15px;
  background: transparent;
}

.captcha-image {
  height: 40px;
  border-radius: 6px;
  overflow: hidden;
  cursor: pointer;
}

.submit-button {
  width: 100%;
  height: 45px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 6px;
  margin-top: 10px;
}

.back-to-login {
  text-align: center;
  margin-top: 20px;
  color: #666;
  font-size: 14px;
}

.login-link {
  color: #1890ff;
  font-weight: 500;
  margin-left: 5px;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .forgot-password-card {
    width: 90%;
    padding: 30px 20px;
  }

  .logo {
    width: 60px;
    height: 60px;
  }

  .system-name {
    font-size: 20px;
  }
}
</style>
